//
//  ShareListView.m
//  Chamleon-template
//
//  Created by RYAN on 15/9/14.
//
//

#import "ShareListView.h"
#import "AppDelegate.h"

#define SHARE_SINAWEIBO @"share_sinaWeibo@2x"
#define SHARE_WEIXIN_SESSION @"share_wechat_session@2x"
#define SHARE_WEIXIN_TIMELINE @"share_wechat_timeline@2x"
#define SHARE_WEIXIN_FAVORITE @"share_wechat_favorite@2x"
#define SHARE_QQ_FRIENDS @"share_qq@2x"
#define SHARE_QQ_ZONE @"share_qq_zone@2x"
#define SHARE_QQ_FAVORITE @"share_qq_favorite@2x"

#define SHARE_BUTTON_SIZE 0.1875

@interface ShareListView ()

@property (nonatomic, strong) NSMutableArray *items;
@property (nonatomic, strong) CDVInvokedUrlCommand *command;

@end

@implementation ShareListView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)setupItems{
    if (!_items) {
        self.items = [[NSMutableArray alloc] init];
    }
    
    [self.items addObject:SHARE_SINAWEIBO];
    [self.items addObject:SHARE_WEIXIN_SESSION];
    [self.items addObject:SHARE_WEIXIN_TIMELINE];
    [self.items addObject:SHARE_WEIXIN_FAVORITE];
    [self.items addObject:SHARE_QQ_FRIENDS];
    [self.items addObject:SHARE_QQ_ZONE];
    [self.items addObject:SHARE_QQ_FAVORITE];
}

- (void)addButtons{
    UIView *buttonsView = [[UIView alloc] initWithFrame:self.bounds];
    [buttonsView setBackgroundColor:[UIColor clearColor]];
    CGFloat bottom;
    
    for (int i=0; i<self.items.count; i++) {
        CGFloat origin_X,origin_Y;
        
        CGFloat buttonSize = SHARE_BUTTON_SIZE * self.frame.size.width;
        CGFloat padding =  (self.frame.size.width - buttonSize*3)/3;
        CGFloat edge = padding*0.5;
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        origin_X = edge + (i%3)*buttonSize + (i%3)*padding;
        origin_Y = (i/3)*(buttonSize + edge);
        [button setFrame:CGRectMake(origin_X, origin_Y, buttonSize, buttonSize)];
        
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.items[i]]];

        [iv setFrame:button.bounds];
        [button addSubview:iv];
        
        [buttonsView addSubview:button];
        bottom = origin_Y + buttonSize;
    }
    
    CGRect frame = buttonsView.frame;
    frame.size.height = bottom;
    buttonsView.frame = frame;
    [buttonsView setCenter:self.center];
    [self addSubview:buttonsView];
}

- (void)show:(CDVInvokedUrlCommand *)command{
    //监听是否跳转出程序
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewClicked) name:UIApplicationDidEnterBackgroundNotification object:nil];
    
    [self setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.4]];
    [self setFrame:[UIScreen mainScreen].bounds];
    
    [self setupItems];
    [self addButtons];
    
    self.command = command;
    
    [self addTarget:self action:@selector(viewClicked) forControlEvents:UIControlEventTouchUpInside];
    
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [appDelegate.window addSubview:self];
}

- (void)buttonClicked:(UIButton *)btn{
    switch (btn.tag) {
        case 0://SHARE_SINAWEIBO
            [self.delegate shareToSina:self.command];
            break;
        case 1://SHARE_WEIXIN_SESSION
            [self.delegate shareToWeixinSession:self.command];
            break;
        case 2://SHARE_WEIXIN_TIMELINE
            [self.delegate shareToWeixinTimeline:self.command];
            break;
        case 3:
            [self.delegate shareToWeixinFavorite:self.command];
            break;
        case 4:
            [self.delegate shareToQQFriends:self.command];
            break;
        case 5:
            [self.delegate shareToQQZone:self.command];
            break;
        case 6:
            [self.delegate shareToQQFavorites:self.command];
            break;
            
        default:
            break;
    }
}

- (void)viewClicked{
    self.command = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
    [self removeFromSuperview];
}

@end
